package com.jason.location.net;

import android.util.Log;
import com.jason.location.listener.ConnectSocketListener;

import java.io.*;
import java.net.Socket;

/**
 * @ClassName: ClientManager
 * @Description: 开启Socket交互管理
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015年11月23日13:09:24
 */
public class ClientManager {

    private static final String TAG = ClientManager.class.getSimpleName();

    private static final ClientManager clientManager = new ClientManager();

    //Socket ip
    private String ip;
    //Socket 端口
    private int port;
    private ConnectSocketListener connectSocketListener;

    private Socket socket;

    private BufferedReader reader;

    private PrintWriter writer;

    private ClientManager(){

    }

    public synchronized static ClientManager getInstance(){
        return clientManager;
    }

    /***
     * 开启连接
     * @param ip    ip
     * @param port  端口
     */
    public void connect(final String ip, final int port,ConnectSocketListener socketListener){
        this.ip = ip;
        this.port = port;
        this.connectSocketListener = socketListener;
        connectSocket();
    }

    /**
     * 连接Socket
     */
    private void connectSocket(){
        new Thread(){

            @Override
            public void run() {
                try {

                    if (socket != null && socket.isConnected() && !socket.isClosed()){
                        return;
                    }
                    if (socket != null){
                        closeSocket();
                    }
                    socket = new Socket(ip,port);
                    writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
                    reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    connectSocketListener.onConnectionSuccess();
                    String line ;
                    while ((line = reader.readLine()) != null){
                        //TODO 读取数据，根据读取到的数据解析客户端的动作(从服务器读取数据)
                        Log.i(TAG,"connectSocket receive message:" + line);
                        connectSocketListener.onReceiveSuccess(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    closeSocket();
                    connectSocketListener.onConnectionError();
                }
            }
        }.start();
    }

    /***
     * 关闭Socket
     */
    private void closeSocket(){
        try {
            if (socket != null){
                socket.close();
                socket = null;
            }
            if (writer != null){
                writer.close();
                writer = null;
            }
            if (reader != null){
                reader.close();
                reader = null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /***
     * 发送数据到服务器端
     * @param message 内容
     */
    public void send(String message){
        Log.i(TAG,"send message:" + message);
        if (socket == null || !socket.isConnected() || socket.isClosed()){
            connectSocket();
        }
        if (socket != null && socket.isConnected() && !socket.isClosed()){
            try{
                writer.write(message);
                writer.flush();
                connectSocketListener.onSendSuccess(message);
            } catch (Exception e){
                connectSocketListener.onConnectionError();
            }
        }
    }

}
